package com.zhang.service;

import com.github.pagehelper.PageInfo;
import com.zhang.entity.JobDetails;
import com.zhang.entity.QuartzBody;
import com.zhang.entity.vo.AddQuartzBody;
import com.zhang.quartz.QuartzManager;
import org.quartz.JobDataMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Service;

import static com.zhang.common.Constant.*;

/**
 * User: 张
 * Date: 2024-11-28
 * Time: 11:20
 * Description:
 */
@Service
public class QuartzService {
    @Autowired
    private QuartzManager quartzManager;
    @Value("${com.zhang.jobClassName}")
    private String jobClassName;
    @SuppressWarnings("unchecked")
    private static Class<? extends QuartzJobBean> getClass(String classname) throws Exception {
//        classname = "com.zhang.quartz.MyJob";
        Class<?> class1 = Class.forName(classname);
        return (Class<? extends QuartzJobBean>) class1;
    }
    public void addJob(AddQuartzBody addQuartzBody) throws Exception {
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put(SERVER_ID,addQuartzBody.getServerId());
        jobDataMap.put(SCRIPT_TEMPLATE_ID,addQuartzBody.getScriptTemplateId());
        jobDataMap.put(EXECUTE_FREQUENCY,addQuartzBody.getExecuteFrequency());
        QuartzBody quartzBody = new QuartzBody(
                getClass(jobClassName),
                addQuartzBody.getJobName(),
                addQuartzBody.getJobGroupName(),
                addQuartzBody.getJobCron(),
                jobDataMap
        );
        quartzManager.addOrUpdateJob(quartzBody);
    }
    public void updateJobCron(QuartzBody quartzBody){
        quartzManager.addOrUpdateJob(quartzBody);
    }
    public void deleteJob(QuartzBody quartzBody){
        quartzManager.deleteJob(quartzBody);
    }
    public void pauseJob(QuartzBody quartzBody){
        quartzManager.pauseJob(quartzBody);
    }
    public void resumeJob(QuartzBody quartzBody){
        quartzManager.resumeJob(quartzBody);
    }
    public void runAJobNow(QuartzBody quartzBody){
        quartzManager.runAJobNow(quartzBody);
    }
    public PageInfo<JobDetails> queryAllJobBean(int pageNum, int pageSize){
        return quartzManager.queryAllJobBean(pageNum, pageSize);
    }
    public PageInfo<JobDetails> queryRunJon(int pageNum, int pageSize){
        return quartzManager.queryRunJon(pageNum, pageSize);
    }

}
